<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function red() {
            console.log('red');
        }

        function green() {
            console.log('green');
        }

        function yellow() {
            console.log('yellow');
        }

        // 定义一个异步函数delay，用于延迟执行
        async function delay(time) {
            // 返回一个Promise对象，在指定的时间后执行resolve方法
            return new Promise((resolve) => setTimeout(resolve, time));
        }

        // 定义一个异步函数trafficLight
        async function trafficLight(light, time) {
            // 等待time时间
            await delay(time);
            // 调用light函数
            light();
        }

        async function loop() {
            await trafficLight(red, 3000);
            await trafficLight(green, 2000);
            await trafficLight(yellow, 1000);
            await loop();
        }

        loop();
    </script>
</body>

</html>